package com.example.italkapp2.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.italkapp2.R;
import com.example.italkapp2.model.bean.InvitationInfo;
import com.example.italkapp2.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

//邀请信息列表页面的适配器
public class InviteAdapter extends BaseAdapter {
    private Context mContext;
    private List<InvitationInfo> mInvitationInfos=new ArrayList<>();
    private OnInviteListener mOnInviteListener;
    private InvitationInfo invitationInfo;



    public InviteAdapter(Context context,OnInviteListener onInviteListener) {
        //获取该适配器所在的页面
        mContext=context;
        mOnInviteListener=onInviteListener;
    }

    //刷新数据的方法
    public void refresh(List<InvitationInfo> invitationInfos){
        if(invitationInfos!=null &&invitationInfos.size()>=0){
            //每次有新的邀请信息传入时都要清空集合
            mInvitationInfos.clear();
            //再一次性把请求全部加载到listView中
            mInvitationInfos.addAll(invitationInfos);

            //刷新页面,属于适配器的方法
            notifyDataSetChanged();
        }
    }

    //获取数量
    @Override
    public int getCount() {
        return mInvitationInfos == null ? 0:mInvitationInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInvitationInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或创建viewHolder
        ViewHolder holder=null;
        if (convertView==null){//没有的话就创建viewHolder
            holder=new ViewHolder();

            convertView=View.inflate(mContext, R.layout.item_invite,null);
            holder.name=convertView.findViewById(R.id.tv_invite_name);
            holder.reason=convertView.findViewById(R.id.tv_invite_reason);
            holder.accept=convertView.findViewById(R.id.btn_invite_accept);
            holder.reject=convertView.findViewById(R.id.btn_invite_reject);

            convertView.setTag(holder);
        }else{//有的话就获取viewHolder
            holder= (ViewHolder) convertView.getTag();
        }

        //获取当前item数据
        invitationInfo = mInvitationInfos.get(position);

        //显示当前item数据
        UserInfo user = invitationInfo.getUser();

        if(user!=null){//是联系人的邀请信息
            //展示名称
            holder.name.setText(invitationInfo.getUser().getName());

            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);

            //展示原因
            if(invitationInfo.getStatus()==InvitationInfo.InvitationStatus.NEW_INVITE) {//新的邀请
                if(invitationInfo.getReason()==null){
                    holder.reason.setText("添加好友");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }
                holder.accept.setVisibility(View.VISIBLE);
                holder.reject.setVisibility(View.VISIBLE);

            }else if(invitationInfo.getStatus()==InvitationInfo.InvitationStatus.INVITE_ACCEPT){//接受邀请
                if(invitationInfo.getReason()==null){
                    holder.reason.setText("接受邀请");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }

            }else if(invitationInfo.getStatus()==InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER) {//邀请被接受
                if(invitationInfo.getReason()==null){
                    holder.reason.setText("发送的邀请已被接受");
                }else{
                    holder.reason.setText(invitationInfo.getReason());
                }
            }

            //接受按钮的处理
            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onAccept(invitationInfo);
                }
            });
            //拒绝按钮的处理
            holder.reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.OnReject(invitationInfo);
                }
            });

        }else{//是群组的邀请信息
            //显示名称
            holder.name.setText(invitationInfo.getGroupInfo().getInvitePerson());

            //显示原因
            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);

            // 显示原因
            switch(invitationInfo.getStatus()){
                // 您的群申请请已经被接受
                case GROUP_APPLICATION_ACCEPTED:
                    holder.reason.setText("您的群申请请已经被接受");
                    break;
                //  您的群邀请已经被接收
                case GROUP_INVITE_ACCEPTED:
                    holder.reason.setText("您的群邀请已经被接收");
                    break;

                // 你的群申请已经被拒绝
                case GROUP_APPLICATION_DECLINED:
                    holder.reason.setText("你的群申请已经被拒绝");
                    break;

                // 您的群邀请已经被拒绝
                case GROUP_INVITE_DECLINED:
                    holder.reason.setText("您的群邀请已经被拒绝");
                    break;

                // 您收到了群邀请
                case NEW_GROUP_INVITE:
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);

                    // 接受邀请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteAccept(invitationInfo);
                        }
                    });

                    // 拒绝邀请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteReject(invitationInfo);
                        }
                    });

                    holder.reason.setText("您收到了群邀请");
                    break;

                // 您收到了群申请
                case NEW_GROUP_APPLICATION:
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);

                    // 接受申请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationAccept(invitationInfo);
                        }
                    });

                    // 拒绝申请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationReject(invitationInfo);
                        }
                    });

                    holder.reason.setText("您收到了群申请");
                    break;

                // 你接受了群邀请
                case GROUP_ACCEPT_INVITE:
                    holder.reason.setText("你接受了群邀请");
                    break;

                // 您批准了群申请
                case GROUP_ACCEPT_APPLICATION:
                    holder.reason.setText("您批准了群申请");
                    break;

                // 您拒绝了群邀请
                case GROUP_REJECT_INVITE:
                    holder.reason.setText("您拒绝了群邀请");
                    break;

                // 您拒绝了群申请
                case GROUP_REJECT_APPLICATION:
                    holder.reason.setText("您拒绝了群申请");
                    break;
            }

        }

        //返回view
        return convertView;
    }

    private class ViewHolder{
        private TextView name;
        private TextView reason;
        private Button accept;
        private Button reject;
    }

    public interface OnInviteListener{
        //联系人接受按钮的点击事件
        void onAccept(InvitationInfo invitationInfo);
        //联系人拒绝按钮的点击事件
        void OnReject(InvitationInfo invitationInfo);

        // 接受邀请按钮处理
        void onInviteAccept(InvitationInfo invationInfo);
        // 拒绝邀请按钮处理
        void onInviteReject(InvitationInfo invationInfo);

        // 接受申请按钮处理
        void onApplicationAccept(InvitationInfo invationInfo);
        // 拒绝申请按钮处理
        void onApplicationReject(InvitationInfo invationInfo);
    }
}
